// const messageUtil = require('../../utils/messageUtil.js')
import messageUtil from "../../utils/messageUtil"
import mobileApi from "../../api/mobileApi"
import commonData from "../../api/data"
import keepOrderApi from "../../api/keepOrderApi"

Component({
  mixins: [],
  data: {
    formData: {
      username: '',
      phone: '',
      code: '',
      isAgreement: false,
    },
    isShowAgreement: false,
    agreementType: 1,
    isShowAgreementConfirm: false,
    isShowSmsConfirm: false,
  },
  props: {
    rootImagePath: ''
  },
  didMount() {
    // this.$page.keepAgreementParent = this
  },
  didUpdate() {},
  didUnmount() {},
  methods: {
    getAuthPhoneNumber(e) {
      console.log('getAuthPhoneNumber', e)

      mobileApi.getAuthorizeMobile(e.detail.encryptedData, e.detail.sign, mobile => {
        console.log('mobile', mobile)

        let formData = this.data.formData
        formData.phone = mobile
        this.setData({
          formData: formData
        })

        this.submitMethod()
      }, err => {
        messageUtil.showFail('获取手机号失败')
        // // todo 测试使用
        // let formData = this.data.formData
        // formData.phone = '13880104833'
        // this.setData({
        //   formData: formData
        // })
        // commonData.setA('666666')
        // commonData.setOpenid("123456")
        // console.log('set default phone', this.data.formData, this.data.formData.phone)
        // this.submitMethod()

      })


      // my.getPhoneNumber({
      //   success: (res) => {
      //     let response = res.response;
      //     response = JSON.parse(response)
      //     console.log('encryptedData', response)

      //     mobileApi.getAuthorizeMobile(response.response, response.sign, mobile => {
      //       console.log('mobile', mobile)

      //       let formData = this.data.formData
      //       formData.phone = mobile
      //       this.setData({
      //         formData: formData
      //       })

      //       this.submitMethod()
      //     }, err => {
      //       messageUtil.showFail('获取手机号失败')
      //       // todo 测试使用
      //       // let formData = this.data.formData
      //       // formData.phone = '13880104833'
      //       // this.setData({
      //       //   formData: formData
      //       // })
      //       // commonData.setA('666666')
      //       // commonData.setOpenid("123456")
      //       // console.log('set default phone', this.data.formData, this.data.formData.phone)
      //       // this.submitMethod()

      //     })

      //   },
      //   fail: (res) => {
      //     console.log('my.getPhoneNumber' + res);
      //   },
      // });


    },
    handleAuthError(e) {
      console.log('getPhoneNumber handleAuthError', e)
    },
    agreementConfirmOkHandler() {
      let formData = this.data.formData
      formData.isAgreement = true

      this.setData({
        formData: formData,
        isShowAgreementConfirm: false
      })
      this.submitMethod()
    },
    agreementConfirmCancelHandler() {
      this.setData({
        isShowAgreementConfirm: false
      })
    },
    agreementConfirmShowAgreementHandler(value) {
      let data = {
        target: {
          dataset: {
            attr: value
          }
        }
      }
      this.showAgreementHandler(data)
    },
    submitMethod() {

      // var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      // if (!(reg_tel.test(this.data.formData.phone))) {
      //   messageUtil.showFail('手机号格式不正确');
      //   return;
      // }


      // formData.value.isAgreement = true;
      if (!this.data.formData.isAgreement) {
        // messageUtil.showFail('请先同意并阅读相关条款')
        console.log('submitMethod2')
        this.setData({
          isShowAgreementConfirm: true
        })
        return;
      }


      console.log('submit', this.data.formData)
      let params = {
        a: commonData.getA(),
        phone: this.data.formData.phone,
        param: commonData.getParam(),
        openid: commonData.getOpenid(),
        // returnUrl: 'http://keep.syhy000.com/keep/five/?mobile=' + this.data.formData.phone,
      }

      let query = "mobile=" + this.data.formData.phone

      params.returnUrl = 'http://keep.syhy000.com/h5GotoZfbMini/#/?appid=2021004135681441&url=pages/keeplq/keeplq&query=' + encodeURIComponent(query)
      messageUtil.showLoading()
      keepOrderApi.submitOrder(params, res => {
        messageUtil.closeLoading()

        my.ap.openURL({
          url: res.data.paySchema, // 请将 url 替换为有效的页面地址
          success: (res) => {
            console.log('openURL success', res)
          },
          fail: (err) => {
            console.log('openURL success', err)
          }
        });
      }, err => {

      })
    },
    inputHandler(e) {
      this.setData({
        formData: {
          phone: e.detail.value
        },
      });
    },
    agreementChange(e) {
      this.setData({
        formData: {
          isAgreement: !this.data.formData.isAgreement
        }
      })
    },
    showAgreementHandler(e) {
      this.setData({
        agreementType: parseInt(e.target.dataset.attr),
        isShowAgreement: true
      })
    },
    agreementOkHandler() {
      this.setData({
        isShowAgreement: false
      })
    },
  },
});